package so.shanku.essential.getdata;

import android.os.Handler;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import so.shanku.essential.R;
import so.shanku.essential.utils.LogUtil;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

public class GetData {
	public static void doPost(Handler handler, String url,
			List<NameValuePair> params, int mark) {

		// http地址
		String strResult;
		HttpPost httpRequest = null;
		try {
            if (params == null) {
				params = new ArrayList<NameValuePair>();
			}

			// HttpGet连接对象
			HttpClient httpClient = new DefaultHttpClient();  
			httpRequest = new HttpPost(url);
			httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			LogUtil.d("GetData", httpRequest+"");
			// 请求HttpClient，取得HttpResponse
			HttpResponse httpResponse = httpClient.execute(httpRequest);
			// 请求成功
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				LogUtil.i("code=", httpResponse.getStatusLine()+"");
				// 取得返回的字符串
				strResult = EntityUtils.toString(httpResponse.getEntity());
//				strResult = EntityUtils.toString(httpResponse.getEntity(), "GBK");
				LogUtil.d("GetData", "strResult:" + strResult);
				if (mark!=800) {
					strResult = strResult.substring(strResult.indexOf("{"),
							strResult.lastIndexOf("}") + 1);
				} 
				
				handler.sendMessage(handler.obtainMessage(
						R.id.msgResultSuccess, mark, 0, strResult));
//				Log.d("GetData", "strResult:" + msg.obj);
			} else {
				handler.sendEmptyMessage(R.id.msgResultFailure);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			handler.sendEmptyMessage(R.id.msgTimeOut);
			LogUtil.e("GetData", "1printStackTrace:" + e);
		} catch (ConnectTimeoutException e) {
			e.printStackTrace();
			handler.sendEmptyMessage(R.id.msgTimeOut);
            LogUtil.e("GetData", "2printStackTrace:" + e);
		} catch (ConnectException e) {
			e.printStackTrace();
			handler.sendEmptyMessage(R.id.msgTimeOut);
            LogUtil.e("GetData", "3printStackTrace:" + e);
		} catch (SocketException e) {
			e.printStackTrace();
			handler.sendEmptyMessage(R.id.msgTimeOut);
            LogUtil.e("GetData", "4printStackTrace:" + e);
		} catch (IOException e) {
			e.printStackTrace();
			handler.sendEmptyMessage(R.id.msgServerError);
            LogUtil.e("GetData", "5printStackTrace:" + e);
		} catch (Exception e) {
			e.printStackTrace();
			handler.sendEmptyMessage(R.id.msgServerError);
            LogUtil.e("GetData", "6printStackTrace:" + e);
		} finally {
			httpRequest.abort();
		}

	}
}
